/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cms.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.cms.entity.HomePicture;
import com.thinkgem.jeesite.modules.cms.service.HomePictureService;

/**
 * 首页滚动图添加Controller
 * @author 张朋
 * @version 2016-10-16
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/homePicture")
public class HomePictureController extends BaseController {

	@Autowired
	private HomePictureService homePictureService;
	
	@ModelAttribute
	public HomePicture get(@RequestParam(required=false) String id) {
		HomePicture entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = homePictureService.get(id);
		}
		if (entity == null){
			entity = new HomePicture();
		}
		return entity;
	}
	
	@RequiresPermissions("cms:homePicture:view")
	@RequestMapping(value = {"list", ""})
	public String list(HomePicture homePicture, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HomePicture> page = homePictureService.findPage(new Page<HomePicture>(request, response), homePicture); 
		model.addAttribute("page", page);
		return "modules/cms/homePictureList";
	}

	@RequiresPermissions("cms:homePicture:view")
	@RequestMapping(value = "form")
	public String form(HomePicture homePicture, Model model) {
		model.addAttribute("homePicture", homePicture);
		return "modules/cms/homePictureForm";
	}

	@RequiresPermissions("cms:homePicture:edit")
	@RequestMapping(value = "save")
	public String save(HomePicture homePicture, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, homePicture)){
			return form(homePicture, model);
		}
		homePictureService.save(homePicture);
		addMessage(redirectAttributes, "保存操作成功");
		return "redirect:"+Global.getAdminPath()+"/cms/homePicture/?repage";
	}
	
	@RequiresPermissions("cms:homePicture:edit")
	@RequestMapping(value = "delete")
	public String delete(HomePicture homePicture, RedirectAttributes redirectAttributes) {
		homePictureService.delete(homePicture);
		addMessage(redirectAttributes, "删除操作成功");
		return "redirect:"+Global.getAdminPath()+"/cms/homePicture/?repage";
	}

}